<?php
class LoginUser{
	public static function isLogin($module){
		if (!isset(Yii::app()->session['LOGIN_USER'])){
			return false;
		}else{
			if 	(Yii::app()->session['LOGIN_USER'] == null || Yii::app()->session['LOGIN_USER'] == ""){
				return false;
			}else{
				//cek user login match with module
				$userLogin = Yii::app()->session['LOGIN_USER'];
				
				//print_r($userLogin);
				//exit;
				
				if (strtoupper($userLogin->module) != strtoupper($module)){
					return false;
				} 
				
				//cek expired time 				
				if  (Yii::app()->session['LOGIN_EXPIRE'] < time()){
					//expired
					return false;
				}else{
					//renew expired time
					//Yii::app()->session['LOGIN_EXPIRE'] = time() + (30 * 60);
					Yii::app()->session['LOGIN_EXPIRE'] = time() + Yii::app()->params['loginTimeout'] ;
					return true;
				}
			}
		}		
	}
	
	public static function getUser(){
		return Yii::app()->session['LOGIN_USER'];	
	}
	
	public static function logout(){
		Yii::app()->session->destroy();
		/*
		if (session_id() ==""){
			session_start();
		}
		session_destroy();
		*/
	}
	
	public static function redirectToLoginPage($module){
		if ($module == "akademik"){
			echo "<script>window.top.location.href='index.php?r=akademik/login'</script>";
		}elseif ($module == "karyawan"){	
			echo "<script>window.top.location.href='index.php?r=karyawan/login'</script>";				
		}elseif ($module == "finance"){
			echo "<script>window.top.location.href='index.php?r=keuangan/login'</script>";
		}
	}
	
	public static function getPermission($module,$user = null,$autoRedirect =true){
		$permit = LoginUser::cekPermission($module,$user);
		if ($permit === false || $permit == 0){
			if ($autoRedirect){
				Yii::app()->request->redirect("index.php?r=permission/notAllowed");
				exit;
			}else{
				return false;
			}
		}else{
			return true;
		}
		
	}
	
	private static function cekPermission($module,$user = null){
		if ($user == null){
			$user = LoginUser::getUser();			
		}		
		if ($user == null){
			return false;
		}else{
			// Cek untuk user_permission
			$cekModule = $module;
			while ($cekModule != ""){
				$permission = MPermission::model()->find("menu_id = '$cekModule' and user_group_name = '". $user["user_name"] ."' and permission_type ='USER'");
				if ($permission != null){
					//echo "Permition for $cekModule : ". $permission["is_permit"] ."</br>";
					return $permission["is_permit"];
				}
				//echo "Permition for $cekModule : false</br>";				
				$pos = strrpos($cekModule, '_');
				if ($pos === false || $pos == 0){
					$cekModule ="";
				}else{
					$cekModule =substr($cekModule,0,$pos);;
				}
			}
			
			// Cek untuk group_permission
			$cekModule = $module;
			while ($cekModule != ""){
				
				//echo "menu_id = '$cekModule' and user_group_name = '". $user["group_account"] ."' permission_type ='GROUP'</br>";
				$permission = MPermission::model()->find("menu_id = '$cekModule' and user_group_name = '". $user["group_account"] ."' and permission_type ='GROUP'");
				
				//echo "Group Permition for $cekModule : ";
				if ($permission != null){
					//echo " true</br>";
					return $permission["is_permit"];
				}
				//echo " false</br>";
				
				$pos = strrpos($cekModule, '_');
				if ($pos === false || $pos == 0){
					$cekModule ="";
				}else{
					$cekModule =substr($cekModule,0,$pos);;
				}
			}
				
			return false;
				 
		}
	}
	
}

?>